package com.tr.rentcar.controller.userc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;

import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.dao.UserDao;

/**
 * Servlet implementation class UserPass
 */
@WebServlet("/user/pass")
public class UserPasssController extends HttpServlet {
	UserDao ud=new UserDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/jsp/user_pass.jsp").forward(request,response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String passj=DigestUtils.md5Hex(request.getParameter("passj"));
		String passxx=request.getParameter("passxx");
		User user=(User) request.getSession().getAttribute("userinfo");
		PrintWriter out=response.getWriter();
		try {
			User users = ud.findByid(user.getUid());
			if (!StringUtils.isEmpty(passxx)) {
				out.println(ud.xgm(DigestUtils.md5Hex(passxx), user.getUid()));
				user.setUpassword(passxx);
				request.getSession().setAttribute("userinfo", user);;
				return;
			}
			if (!users.getUpassword().equals(passj)) {
				out.println(false);
				return;
			}else {
				out.println(true);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
